<?php
/**  此控制器用来解决跨域 */
namespace app\common\controller;

use think\Controller;


class Base extends Controller
{
    /** 解决跨域    */
    protected function _initialize()
    {
        parent::_initialize();
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, POST, PATCH, PUT, DELETE');
        header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, token, Accept, x-access-sign, x-access-time');
        if(request()->isOptions()){
            exit();
        }
    }

    /**
     * 通用响应
     * @param $code
     * @param $msg
     * @param $data
     * @return void
     */
    public function response($code, $msg , $data=[] )
    {
        $res = ["code" => $code, "msg" => $msg, "data" => $data];
        json($res)->send();
        die();
    }

    /**
     * 失败时候的响应
     * @param $msg
     * @param $code
     * @return null
     */
    public function fail($msg = 'fail', $code = 500)
    {
        return $this->response($code, $msg);
    }

    /**
     * 成功时候的响应
     * @param $code
     * @param $msg
     * @param $data
     * @return null
     */
    public function correct( $data = [],$msg = 'success', $code = 200)
    {
        return $this->response($code, $msg, $data);
    }
}
